package org.jeecg.modules.basic.plant.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.basic.plant.entity.WmsSapWarehouseMap;
import org.jeecg.modules.basic.plant.mapper.WmsSapWarehouseMapMapper;
import org.jeecg.modules.basic.plant.service.IWmsSapWarehouseMapService;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouse;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Description: 仓库和sap库位对照信息
 * @Author: jeecg-boot
 * @Date: 2023-10-16
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class WmsSapWarehouseMapServiceImpl extends ServiceImpl<WmsSapWarehouseMapMapper, WmsSapWarehouseMap> implements IWmsSapWarehouseMapService {

    private final IWmsBasicWarehouseService wmsBasicWarehouseService;

    private final WmsSapWarehouseMapMapper sapWarehouseMapMapper;

    @Override
    public void resave(WmsSapWarehouseMap wmsSapWarehouseMap) {
        WmsBasicWarehouse warehouse = wmsBasicWarehouseService.getById(wmsSapWarehouseMap.getWmsBasicWarehouseId());
        if (ObjectUtil.isNotEmpty(warehouse)) {
            wmsSapWarehouseMap.setWmsBasicWarehouseCode(warehouse.getCode());
            wmsSapWarehouseMap.setSysOrgCode(warehouse.getSysOrgCode());
        }
        this.save(wmsSapWarehouseMap);
    }

    @Override
    public String getSapStoreCode(String storeCode) {
        List<WmsSapWarehouseMap> warehouses = this.list(new LambdaQueryWrapper<WmsSapWarehouseMap>().eq(WmsSapWarehouseMap::getWmsBasicWarehouseCode, storeCode));
        Optional<WmsSapWarehouseMap> first = warehouses.stream().findFirst();
        if (first.isPresent()) {
            return first.get().getSapLocation();
        }

        throw new JeecgBootException("当前仓库编码未与sap的仓库编码对应");
    }

    @Override
    public void updatePlant(WmsSapWarehouseMap wmsSapWarehouseMap) {
        WmsBasicWarehouse warehouse = wmsBasicWarehouseService.getById(wmsSapWarehouseMap.getWmsBasicWarehouseId());
        if (ObjectUtil.isNotEmpty(warehouse)) {
            wmsSapWarehouseMap.setWmsBasicWarehouseCode(warehouse.getCode());
        }
        this.updateById(wmsSapWarehouseMap);
    }

    /**
     * 分页
     * @param page
     * @param sapWarehouseMap
     * @return
     */
    @Override
    public Page<WmsSapWarehouseMap> listByPage(Page<WmsSapWarehouseMap> page, WmsSapWarehouseMap sapWarehouseMap) {
        return page.setRecords(sapWarehouseMapMapper.listByPage(page, sapWarehouseMap));
    }

    @Override
    public String getWmsStroeBySapStore(String storeLocation,String factory) {
        List<WmsSapWarehouseMap> wmsSapWarehouseMapList = this.list(new LambdaQueryWrapper<WmsSapWarehouseMap>()
                .eq(WmsSapWarehouseMap::getSapLocation, storeLocation)
                .eq(WmsSapWarehouseMap::getFactory,factory)
                .eq(WmsSapWarehouseMap::getState, "1"));
        return CollectionUtil.isNotEmpty(wmsSapWarehouseMapList) ? wmsSapWarehouseMapList.get(0).getWmsBasicWarehouseId() : null;
    }
}
